﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace NJL.WebManage.Citys
{
    public partial class AddCity : BasePage
    {
        NJL.BLL.NJL_CS_AREA bllArea = new BLL.NJL_CS_AREA();
        NJL.Model.NJL_CS_AREA modelArea;
        NJL.BLL.NJL_CUSTOM_AREA bllCustomArea = new BLL.NJL_CUSTOM_AREA();
        NJL.Model.NJL_CUSTOM_AREA modelCustomArea;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataSet ds;
                if (WEBSITE_CODE == "1")
                {
                    ds = bllArea.GetList("AREA_CODE like '%0000'");
                    ddlProvince.DataSource = ds;
                    ddlProvince.DataTextField = "AREA_NAME";
                    ddlProvince.DataValueField = "AREA_CODE";
                    ddlProvince.DataBind();
                    ddlSearchProvince.DataSource = ds;
                    ddlSearchProvince.DataTextField = "AREA_NAME";
                    ddlSearchProvince.DataValueField = "AREA_CODE";
                    ddlSearchProvince.DataBind();
                }
                else
                {
                    ddlProvince.Visible = false;
                    ddlSearchProvince.Visible = false;
                    string ProvinceId = WEBSITE_CODE.Substring(0, 2);
                    DataSet dscity = bllArea.GetList("AREA_CODE like '" + ProvinceId + "%00' and deep=2");
                    ddlCity.DataSource = dscity;
                    ddlCity.DataTextField = "AREA_NAME";
                    ddlCity.DataValueField = "AREA_CODE";
                    ddlCity.DataBind();
                }
                Bind();

            }
            else
            {
                BindEvent();//注册事件
            }

        }
        protected string sql()
        {
            string sqlStr = "CUSTOM_AREA_NAME like '%"+txtKeywords.Text.Trim()+"%'";
            if (WEBSITE_CODE != "1")
            {
                sqlStr += " and NJL_SYS_BASE_ID='" + WEBSITE_CODE + "'";
            }
            else
            {
                if (ddlSearchProvince.SelectedValue != "000000")
                {
                    sqlStr += "and NJL_SYS_BASE_ID='" + ddlSearchProvince.SelectedValue + "'";
                }
            }
            return sqlStr;
        }
        protected void Bind()
        {
            #region 地区列表
            DataSet ds = bllCustomArea.GetList(sql());
            rptUserList.DataSource = ds;
            BindItemDataBound();
            rptUserList.DataBind();
            #endregion
        }
        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ProvinceId = ddlProvince.SelectedValue.Substring(0, 2);
            DataSet ds = bllArea.GetList("AREA_CODE like '" + ProvinceId + "%00' and deep=2");
            ddlCity.DataSource = ds;
            ddlCity.DataTextField = "AREA_NAME";
            ddlCity.DataValueField = "AREA_CODE";
            ddlCity.DataBind();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            modelCustomArea = new Model.NJL_CUSTOM_AREA();
            if (ViewState["ID"] != null)
            {
                modelCustomArea = bllCustomArea.GetModel(ViewState["ID"].ToString());
                modelCustomArea.SORT = Convert.ToInt32(txtSort.Text.Trim());
                modelCustomArea.CITY_CODE = ddlCity.SelectedValue;
                modelCustomArea.CUSTOM_AREA_NAME = txtAreaName.Text.Trim();
                modelCustomArea.UPDATE_USER_ID = USER_ID;
                modelCustomArea.UPDATE_USER_NAME = USER_NAME;
                modelCustomArea.UPDATE_DATE_TIME = DateTime.Now;
                if (bllCustomArea.Update(modelCustomArea))
                {
                    Royalsoft.Common.MessageBox.ShowAndRedirect(this, "更新成功", Request.RawUrl);
                }

            }
            else
            {

                modelCustomArea.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                if (WEBSITE_CODE == "1")
                {
                    modelCustomArea.NJL_SYS_BASE_ID = ddlProvince.SelectedValue;
                }
                else
                {
                    modelCustomArea.NJL_SYS_BASE_ID = WEBSITE_CODE;
                }
                modelCustomArea.SORT = Convert.ToInt32(txtSort.Text.Trim());
                modelCustomArea.CITY_CODE = ddlCity.SelectedValue;
                modelCustomArea.CUSTOM_AREA_NAME = txtAreaName.Text.Trim();
                modelCustomArea.CREATE_USER_ID = USER_ID;
                modelCustomArea.CREATE_USER_NAME = USER_NAME;
                modelCustomArea.CREATE_DATE_TIME = DateTime.Now;
                if (bllCustomArea.Add(modelCustomArea))
                {
                    Royalsoft.Common.MessageBox.ShowAndRedirect(this, "添加成功", Request.RawUrl);
                }
            }
        }
        protected void rptUserList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal litArea = (Literal)e.Item.FindControl("litArea");
                DataRowView drv = (DataRowView)e.Item.DataItem;
                string areaCode = drv["CITY_CODE"].ToString();
                modelArea = bllArea.GetModel(areaCode);
                DataSet ds = bllArea.GetList("AREA_CODE = '" + areaCode.Substring(0, 2) + "0000'");
                litArea.Text =  modelArea.AREA_NAME+"——"+ ds.Tables[0].Rows[0]["AREA_NAME"].ToString();

            }
        }
        protected void rptUserList_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                btnCel.Visible = true;
                ViewState["ID"] = e.CommandArgument.ToString();
                string id = e.CommandArgument.ToString();
                modelCustomArea = bllCustomArea.GetModel(id);
                txtSort.Text = modelCustomArea.SORT.ToString();
                txtAreaName.Text = modelCustomArea.CUSTOM_AREA_NAME;
                if (WEBSITE_CODE != "1")
                {
                    ddlProvince.Visible = false;
                    string ProvinceId = WEBSITE_CODE.Substring(0, 2);
                    DataSet dscity = bllArea.GetList("AREA_CODE like '" + ProvinceId + "%00' and deep=2");
                    ddlCity.DataSource = dscity;
                    ddlCity.DataTextField = "AREA_NAME";
                    ddlCity.DataValueField = "AREA_CODE";
                    ddlCity.DataBind();
                }
                else
                {
                    string ProvinceId = modelCustomArea.CITY_CODE.Substring(0, 2);
                    DataSet ds = bllArea.GetList("AREA_CODE like '" + ProvinceId + "%00' and deep=2");
                    ddlCity.DataSource = ds;
                    ddlCity.DataTextField = "AREA_NAME";
                    ddlCity.DataValueField = "AREA_CODE";
                    ddlCity.DataBind();
                    ds = bllArea.GetList("AREA_CODE like '%0000'");
                    ddlProvince.DataSource = ds;
                    ddlProvince.DataTextField = "AREA_NAME";
                    ddlProvince.DataValueField = "AREA_CODE";
                    ddlProvince.DataBind();
                    ds = bllArea.GetList("AREA_CODE like '" + ProvinceId + "0000'");
                    ddlProvince.SelectedValue = ds.Tables[0].Rows[0]["AREA_CODE"].ToString();

                }
                ddlCity.SelectedValue = modelCustomArea.CITY_CODE;

            }
            else if (e.CommandName == "Del")
            {
                string id = e.CommandArgument.ToString();
                if (bllCustomArea.Delete(id))
                {
                    Bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }

        }
        void BindEvent()
        {
            rptUserList.ItemCommand += new RepeaterCommandEventHandler(this.rptUserList_ItemCommand);
        }
        void BindItemDataBound()
        {
            rptUserList.ItemDataBound += new RepeaterItemEventHandler(rptUserList_ItemDataBound);
        }
        protected void btnDel_Click1(object sender, EventArgs e)
        {
            string[] cbxid = HSelectId.Value.Split(',');
            foreach (string id in cbxid)
            {
                modelCustomArea = bllCustomArea.GetModel(id);
                if (bllCustomArea.Delete(id))
                {
                    Bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Bind();
        }

    }
}